import { defineStore } from 'pinia';
import AppState, { UserState } from './types';

const useAppStore = defineStore('app', {
  state: (): AppState => ({
    theme: 'light',
    isLogin: false,
    roles: [],
    user: {
      id: '',
      name: '',
      mobile: '',
      avatar: '',
    },
  }),

  actions: {
    setTheme(theme: 'light' | 'dark') {
      this.theme = theme;
      document.body.setAttribute('data-theme', theme);
      if (theme === 'dark') {
        document.body.setAttribute('arco-theme', 'dark');
      } else {
        document.body.removeAttribute('arco-theme');
      }
    },
    setIsLogin(val: boolean) {
      this.isLogin = val;
    },
    setRoles(val: string[]) {
      this.roles = val;
    },
    patchRoles(val: string) {
      this.roles.push(val);
    },
    setUser(val: UserState) {
      this.user = val;
    },
    resetUser() {
      this.user = { id: '', name: '', mobile: '', avatar: '' };
    },
  },
});

export default useAppStore;
